package b.n;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

/* compiled from: ClassesInfoCache */
public class a {
    public static a c = new a();
    public final Map<Class, a> a = new HashMap();
    public final Map<Class, Boolean> b = new HashMap();

    /* compiled from: ClassesInfoCache */
    public static class a {
        public final Map<b.n.d.a, List<b>> a = new HashMap();
        public final Map<b, b.n.d.a> b;

        public a(Map<b, b.n.d.a> map) {
            this.b = map;
            for (Entry entry : map.entrySet()) {
                b.n.d.a aVar = (b.n.d.a) entry.getValue();
                List list = (List) this.a.get(aVar);
                if (list == null) {
                    list = new ArrayList();
                    this.a.put(aVar, list);
                }
                list.add(entry.getKey());
            }
        }

        public static void a(List<b> list, g gVar, b.n.d.a aVar, Object obj) {
            if (list != null) {
                int size = list.size() - 1;
                while (size >= 0) {
                    b bVar = (b) list.get(size);
                    Objects.requireNonNull(bVar);
                    try {
                        int i = bVar.a;
                        if (i == 0) {
                            bVar.b.invoke(obj, new Object[0]);
                        } else if (i == 1) {
                            bVar.b.invoke(obj, new Object[]{gVar});
                        } else if (i == 2) {
                            bVar.b.invoke(obj, new Object[]{gVar, aVar});
                        }
                        size--;
                    } catch (InvocationTargetException e) {
                        throw new RuntimeException("Failed to call observer method", e.getCause());
                    } catch (IllegalAccessException e2) {
                        throw new RuntimeException(e2);
                    }
                }
            }
        }
    }

    /* compiled from: ClassesInfoCache */
    public static class b {
        public final int a;
        public final Method b;

        public b(int i, Method method) {
            this.a = i;
            this.b = method;
            method.setAccessible(true);
        }

        public boolean equals(Object obj) {
            boolean z = true;
            if (this == obj) {
                return true;
            }
            if (obj == null || b.class != obj.getClass()) {
                return false;
            }
            b bVar = (b) obj;
            if (!(this.a == bVar.a && this.b.getName().equals(bVar.b.getName()))) {
                z = false;
            }
            return z;
        }

        public int hashCode() {
            return this.b.getName().hashCode() + (this.a * 31);
        }
    }

    public final a a(Class cls, Method[] methodArr) {
        Class superclass = cls.getSuperclass();
        HashMap hashMap = new HashMap();
        if (superclass != null) {
            a b = b(superclass);
            if (b != null) {
                hashMap.putAll(b.b);
            }
        }
        for (Class b2 : cls.getInterfaces()) {
            for (Entry entry : b(b2).b.entrySet()) {
                c(hashMap, (b) entry.getKey(), (b.n.d.a) entry.getValue(), cls);
            }
        }
        if (methodArr == null) {
            try {
                methodArr = cls.getDeclaredMethods();
            } catch (NoClassDefFoundError e) {
                throw new IllegalArgumentException("The observer class has some methods that use newer APIs which are not available in the current OS version. Lifecycles cannot access even other methods so you should make sure that your observer classes only access framework classes that are available in your min API level OR use lifecycle:compiler annotation processor.", e);
            }
        }
        boolean z = false;
        for (Method method : methodArr) {
            n nVar = (n) method.getAnnotation(n.class);
            if (nVar != null) {
                int i;
                Class[] parameterTypes = method.getParameterTypes();
                if (parameterTypes.length <= 0) {
                    i = 0;
                } else if (parameterTypes[0].isAssignableFrom(g.class)) {
                    i = 1;
                } else {
                    throw new IllegalArgumentException("invalid parameter type. Must be one and instanceof LifecycleOwner");
                }
                b.n.d.a value = nVar.value();
                if (parameterTypes.length > 1) {
                    if (!parameterTypes[1].isAssignableFrom(b.n.d.a.class)) {
                        throw new IllegalArgumentException("invalid parameter type. second arg must be an event");
                    } else if (value == b.n.d.a.ON_ANY) {
                        i = 2;
                    } else {
                        throw new IllegalArgumentException("Second arg is supported only for ON_ANY value");
                    }
                }
                if (parameterTypes.length <= 2) {
                    c(hashMap, new b(i, method), value, cls);
                    z = true;
                } else {
                    throw new IllegalArgumentException("cannot have more than 2 params");
                }
            }
        }
        a aVar = new a(hashMap);
        this.a.put(cls, aVar);
        this.b.put(cls, Boolean.valueOf(z));
        return aVar;
    }

    public a b(Class cls) {
        a aVar = (a) this.a.get(cls);
        if (aVar != null) {
            return aVar;
        }
        return a(cls, null);
    }

    public final void c(Map<b, b.n.d.a> map, b bVar, b.n.d.a aVar, Class cls) {
        b.n.d.a aVar2 = (b.n.d.a) map.get(bVar);
        if (aVar2 != null && aVar != aVar2) {
            Method method = bVar.b;
            StringBuilder g = c.b.a.a.a.g("Method ");
            g.append(method.getName());
            g.append(" in ");
            g.append(cls.getName());
            g.append(" already declared with different @OnLifecycleEvent value: previous value ");
            g.append(aVar2);
            g.append(", new value ");
            g.append(aVar);
            throw new IllegalArgumentException(g.toString());
        } else if (aVar2 == null) {
            map.put(bVar, aVar);
        }
    }
}
